#include<stdio.h>
#include<at89c51ed2.h>
#include<mcs51reg.h>
#include<malloc.h>


extern void i2c_init(int block_select);
extern void i2c();
extern void i2c_read();
extern int i2c_write();
extern int i2c_sendData(int val);
extern int i2c_readData();
extern void i2c_stop();
extern int i2c_ack();
extern void i2c_noack();
extern void i2c_expandinit();
extern void i2c_reset();
extern void i2c_sendDataPage(int val);





void delay(unsigned char val){
	
	unsigned char i;
	i=0;
	while(i<val)
	{
		i++;
	}
}


///*acknowledge polling*/
void deviceBusy(){
unsigned char ack;

ack=1;
delay(100);
	while(ack==1){
	delay(100);
	i2c_init(0);
	ack=i2c_write();
	
	//ack=i2c_ack();
	//printf("%d",ack);
	}

}


unsigned char i2cexpanderWrite(int val){
	int ack;
	
	i2c_expandinit();
	i2c_write();
	ack=i2c_sendData(val);
	i2c_stop();
	return ack;
}

unsigned char i2cexpanderRead(){
	unsigned char readV;
	
	//delay(100);
	i2c_expandinit();
	i2c_read();
	readV=i2c_readData();
	i2c_stop();
	return readV;
}



		
unsigned char eepageW(int addr){
	int blck_select;
	int ack;
	int i;
	deviceBusy();
	
	if(addr<256){
		blck_select=0;
		
		
	}
	if(addr>255&&addr<512){
		blck_select=1;
		addr&=0x0FF;
		
		
	}
	if(addr>511&&addr<768){
		blck_select=2;
		addr&=0x0FF;
		
		
	}
	if(addr>767&&addr<1024){
		blck_select=3;
		addr&=0x0FF;
		
		
	}
	if(addr>1023&&addr<1280){
		blck_select=4;
		addr&=0x0FF;
		
		
	}
	if(addr>1279&&addr<1536){
		blck_select=5;
		addr&=0x0FF;
		
		
	}
	if(addr>1535&&addr<1792){
		blck_select=6;
		addr&=0x0FF;
		
		
	}
	if(addr>1791&&addr<2049){
		blck_select=7;
		addr&=0x0FF;
		
		
	}
	
	/*send address*/
	

	i2c_init(blck_select);
	i2c_write();

	ack=i2c_sendData(addr);
	for(i=0;i<15;i++){
	ack=i2c_sendData(0x15);
	}
	i2c_stop();
	
	return ack;
}


	
int eebytew(int addr,int value){
	int blck_select;
	int ack;

	deviceBusy();
	
	if(addr<256){
		blck_select=0;
		
		i2c_init(blck_select);
		
	}
	if(addr>255&&addr<512){
		blck_select=1;
		addr&=0x0FF;
		i2c_init(blck_select);
		
	}
	if(addr>511&&addr<768){
		blck_select=2;
		addr&=0x0FF;
		i2c_init(blck_select);
		
	}
	if(addr>767&&addr<1024){
		blck_select=3;
		addr&=0x0FF;
		i2c_init(blck_select);
		
	}
	if(addr>1023&&addr<1280){
		blck_select=4;
		addr&=0x0FF;
		i2c_init(blck_select);
		
	}
	if(addr>1279&&addr<1536){
		blck_select=5;
		addr&=0x0FF;
		i2c_init(blck_select);
		
	}
	if(addr>1535&&addr<1792){
		blck_select=6;
		addr&=0x0FF;
		i2c_init(blck_select);
		
	}
	if(addr>1791&&addr<2049){
		blck_select=7;
		addr&=0x0FF;
		i2c_init(blck_select);
		
	}
	
	/*send address*/
	
	i2c_write();
//	printf("Addres value:%d\n\r",addr);
//	printf("Data value:%d\n\r",value);
	ack=i2c_sendData(addr);
	ack=i2c_sendData(value);
	i2c_stop();
	return ack;
}
		
		
		
int * eebyteSeqr(int addr,int numBytes){
	int blck_select;
	int ack;	
	
	int count;
	xdata int * hexDump;
	
	hexDump=(xdata int *)malloc(1000);
	
	deviceBusy();
	
	
	if(addr<256){
		blck_select=0;
		
		i2c_init(blck_select);
		
	}
	if(addr>255&&addr<512){
		blck_select=1;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>511&&addr<768){
		blck_select=2;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>767&&addr<1024){
		blck_select=3;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1023&&addr<1280){
		blck_select=4;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1279&&addr<1536){
		blck_select=5;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1535&&addr<1792){
		blck_select=6;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1791&&addr<2049){
		blck_select=7;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	
	
	i2c_write();
	ack=i2c_sendData(addr);
	i2c_init(blck_select);
	i2c_read();
	
	
	count=0;
	while(count<numBytes){
	delay(200);	
	hexDump[count]=i2c_readData();
	
	count++;
	i2c_noack();

	}
	ack=i2c_ack();
	i2c_stop();
	return hexDump;
}



	
	

unsigned char eebyter(int addr){
	int blck_select;
	int ack;	
	int readV;
	
		deviceBusy();
	
	if(addr<256){
		blck_select=0;
		
		i2c_init(blck_select);
		
	}
	if(addr>255&&addr<512){
		blck_select=1;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>511&&addr<768){
		blck_select=2;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>767&&addr<1024){
		blck_select=3;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1023&&addr<1280){
		blck_select=4;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1279&&addr<1536){
		blck_select=5;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1535&&addr<1792){
		blck_select=6;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	if(addr>1791&&addr<2049){
		blck_select=7;
		addr&=0xFF;
		i2c_init(blck_select);
		
	}
	
	
	
	
	
	
	i2c_write();
	ack=i2c_sendData(addr);
	i2c_init(blck_select);
	i2c_read();
	readV=i2c_readData();
	ack=i2c_ack();
//	if(ack==1){
//			printf("\n\r\t\t\tRead Success!\n\r\t\t\t");
//		}
//		else{
//			printf("\n\r\t\t\tRead Failed\n\r\t\t\t");
//		}
		
	i2c_stop();
	return readV;

}

void eereset()
{
	i2c_reset();
}	


unsigned char deBouncer(unsigned char IOreader)
{

	unsigned char dBounce;
	
	dBounce=1;
	
		
		if(((IOreader&0x20)!=0)&&((IOreader&0x01)!=0)&&((IOreader&0x02)!=0)&&((IOreader&0x04)!=0)){
			dBounce=0;
		}

	
	return dBounce;
	
}
	
